<?php

namespace Admin\Controller;

use Admin\Form\CatalogingDocumentForm;
use Admin\Controller\IndexController;
use Zend\Form\Annotation\AnnotationBuilder;
use Admin\Model\DBO\CatalogingDocumentDBO;
use Admin\Model\DBO\SearchBookDBO;
use Admin\Entity\Book;
use Admin\Entity\Author;

class CatalogingDocumentController extends IndexController {

    public function getForm() {
        if (!$this->form) {
            $cataloging = new CatalogingDocumentForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($cataloging);
        }

        return $this->form;
    }

    public function insertDataToForm($form, $objectCataloging) {
        $languages = $objectCataloging->getLanguage();
        $bookCategories = $objectCataloging->getBookCategory();
        $bookTypes = $objectCataloging->getBookType();
        $languageOptions = array();
        foreach ($languages as $language) {
            $languageOptions[$language['languageID']] = $language['languageName'];
        }
        $form->get('languageID')->setOptions(
                array(
                    'value_options' => $languageOptions,
                )
        );

        $categoryOptions = array();
        foreach ($bookCategories as $bc) {
            $categoryOptions[$bc['bookCategoryID']] = $bc['bookCategoryName'];
        }
        $form->get('bookCategoryID')->setOptions(
                array(
                    'value_options' => $categoryOptions,
                )
        );

        $typeOptions = array();
        foreach ($bookTypes as $bt) {
            $typeOptions[$bt['bookTypeID']] = $bt['bookTypeName'];
        }
        $form->get('bookTypeID')->setOptions(
                array(
                    'value_options' => $typeOptions,
                )
        );
    }

    public function indexAction() {
        // kiem tra neu chua dang nhap thi quay ve lai trang dang nhap
        $this->checkRole3();
        // Neu dang nhap roi thi hien thi len sach sach nguoi dung
        // Lay thuc the dang nhap dua ra layout
        $identity = $this->getAuthService()->getIdentity();
        $this->layout()->identity = $identity;
        // Hien thi dua sanh sach nguoi dung ra view
        $form = $this->getForm();
        $form->get('bookBarcode')->setAttribute('required', 'required');
        $objectCataloging = new CatalogingDocumentDBO($this->getServiceLocator());
        $this->insertDataToForm($form, $objectCataloging);
        $request = $this->getRequest();
        // Khi bam nut Submit
        if ($request->isPost()) {
            $myMessage = array();
            $dataRequest = $request->getPost();
            $form->setData($dataRequest);
            if ($form->isValid()) {
                $data = $form->getData();
                // Kiem tra da ton tai BookBarcode trong BookStore hay chua
                if ($objectCataloging->getBookBarcode($data['bookBarcode'])) {
                    $myMessage['bookBarcodeExist'] = 'bookBarcodeExist';
                } else {
                    $book = new Book();
                    $author = new Author();
                    $book->setDataCataloging($data);
                    $author->setAuthor($data['author']);
                    $book->setPersonCreated($identity);
                    if ($objectCataloging->saveBook($book, $author)) {
                        $myMessage['catalogingOK'] = 'catalogingOK';
                    } else {
                        $myMessage['saveError'] = 'saveError';
                    }
                }
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'form' => $form,
        );
    }

    public function detailDocumentAction() {
        $bookBarcode = (int) $this->params()->fromRoute('id', 0);
        if (!$bookBarcode) {
            return $this->redirect()->toRoute('search-document/default', array('action' => $action, 'id' => $bookBarcode));
        }
        // Lấy thông tin chi tiết về một tài liệu bao gồm - thông tin biên mục - tổng số tài liệu
        // số tài liệu đang mượn, số tài liệu còn lại trong kho
        $objectCataloging = new CatalogingDocumentDBO($this->getServiceLocator());
        $infoBook = $objectCataloging->getInfoBook($bookBarcode);
        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
        $authors = $objectSearchBook->searchBookAuthor($infoBook['bookBarcode']);
        $stringAuthor = "";
        foreach ($authors as $author) {
            $stringAuthor .= $author['author'] . '-';
        }
        // loại bỏ các ký tự thừa;
        $stringAuthor = trim($stringAuthor);
        $stringAuthor = substr($stringAuthor, 0, strlen($stringAuthor) - 1);
        // Lay toan bo sach cua loai sach trong kho
        $allBook = $objectCataloging->getAllBookFromBarcode($bookBarcode);
        // Lay sach co trong kho
        $allBookInStore = $objectCataloging->getBookExist($bookBarcode);
        return array(
            'infoBook' => $infoBook,
            'stringAuthor' => $stringAuthor,
            'allBook' => $allBook,
            'allBookInStore' => $allBookInStore,
        );
    }

    public function editDocumentAction() {
        // kiem tra neu chua dang nhap thi quay ve lai trang dang nhap
        $this->checkRole3();
        // Lay thuc the dang nhap dua ra layout
        $identity = $this->getAuthService()->getIdentity();
        $this->layout()->identity = $identity;
        $bookBarcode = (int) $this->params()->fromRoute('id', 0);
        if (!$bookBarcode) {
            return $this->redirect()->toRoute('search-document');
        }
        $form = $this->getForm();
        $objectCataloging = new CatalogingDocumentDBO($this->getServiceLocator());
        $this->insertDataToForm($form, $objectCataloging);
        $book = new Book();
        $author = new Author();
        $infoBook = $objectCataloging->getInfoBook($bookBarcode);
        $objectSearchBook = new SearchBookDBO($this->getServiceLocator());
        $authors = $objectSearchBook->searchBookAuthor($infoBook['bookBarcode']);
        $stringAuthor = "";
        $authorID = array();
        foreach ($authors as $author) {
            $stringAuthor .= $author['author'] . '-';
            $authorID[$author['bookAuthorID']] = $author['bookAuthorID'];
        }
        // loại bỏ các ký tự thừa;
        $stringAuthor = trim($stringAuthor);
        $stringAuthor = substr($stringAuthor, 0, strlen($stringAuthor) - 1);
        $book->setDataCataloging($infoBook);
        $form->get('cataloging')->setAttribute('value', 'Chỉnh sửa');
        $form->get('author')->setAttribute('value', $stringAuthor);
        $form->get('bookBarcode')->setAttribute('disabled', 'true');
        $form->get('bookBarcode')->setAttribute('class', 'borrowReturn');
        $form->bind($book);
        $request = $this->getRequest();
        if ($request->isPost()) {
            $myMessage = array();
            $form->setData($request->getPost());
            $form->get('bookBarcode')->removeAttribute('required');
            if ($form->isValid()) {
                if ($objectCataloging->getBookBarcode($bookBarcode))
                    $objectCataloging->updateBookInfo($book, $bookBarcode);
                // Redirect to list of rooms     
                return $this->redirect()->toRoute('search-document/default', array('action' => 'searchBookBarcode', 'id' => $bookBarcode));
            } else {
                foreach ($form->getMessages() as $value => $ms) {
                    $myMessage[$value] = $value;
                }
            }
            return array(
                'form' => $form,
                'bookBarcode' => $bookBarcode,
                'myMessage' => $myMessage,
            );
        }
        return array(
            'bookBarcode' => $bookBarcode,
            'form' => $form,
        );
    }

    public function deleteDocumentAction() {
        return $this->redirect()->toRoute('search-document');
    }

}
